﻿using DGSFramework;
using DGSFramework.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace Framework.Resources
{
    /// <summary>
    /// Summary description for GetStockDataDetails
    /// </summary>
    public class GetStockDataDetails : IHttpHandler
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";
            JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
            var historyDataList = new List<HistoryDataListModel>();
            var status = 0;
            string code = string.Empty;
            try
            {
                code = HttpContext.Current.Request.Form["stock"].ToString();
                var date = HttpContext.Current.Request.Form["getTime"].ToString();
                var currentDate = DateTime.Parse(date);
                var stockDataDateList = entity.dgs_StockDatas.Where(d => d.dgs_Stocks.Code == code && d.GetTime <= currentDate).Select(d => new StockDataDetailModel
                {
                    Price = d.KL_Gia,
                    Volume = d.KL_KL,
                    GetTime = d.GetTime,
                    DM_Gia1 = d.DM_Gia1,
                    DM_KL1 = d.DM_KL1,
                    DM_Gia2 = d.DM_Gia2,
                    DM_KL2 = d.DM_KL2,
                    DM_Gia3 = d.DM_Gia3,
                    DM_KL3 = d.DM_KL3,
                    DB_Gia1 = d.DB_Gia1,
                    DB_KL1 = d.DB_KL1,
                    DB_Gia2 = d.DB_Gia2,
                    DB_KL2 = d.DB_KL2,
                    DB_Gia3 = d.DB_Gia3,
                    DB_KL3 = d.DB_KL3,
                    GiaThamChieu = d.GiaThamChieu,
                    StockCode = d.dgs_Stocks.Code                    
                }).OrderByDescending(d => d.GetTime).Skip(0).Take(3).ToList();

                foreach (var item in stockDataDateList)
                {

                    var currentDataDetails = entity.dgs_StockDataDetailTemp.Where(d => d.StockCode == code && d.GetTime == item.GetTime)
                     .Select(d => new 
                     {                         
                         ArrayValue = d.ArrayValue
                     }).FirstOrDefault();                   

                    historyDataList.Add(new HistoryDataListModel
                    {

                        DateMatched = item.GetTime.Value,                      
                        ArrayValue = currentDataDetails!=null?currentDataDetails.ArrayValue:string.Empty,
                        QueueDetails = item,
                        MaxQueue = item.KhoiLuongList.Count() > 0 ? item.KhoiLuongList.Max(d => d) : 0
                    });

                }
                status = 1;
            }
            catch (Exception e)
            {

                status = -1;
            }

            context.Response.Write(
               jsonSerializer.Serialize(
                   new
                   {
                       Status = status,
                       Code = code,
                       Data = historyDataList
                   }
               )
           );
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}